home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / OLEDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.2 KB  |  315 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of TOleDocument. Doc/View document that supports OLE 2
  8. // using OCF TOcDocument
  9. //----------------------------------------------------------------------------
  10. #define INC_OLE2
  11. #include <owl/pch.h>
  12. #if !defined(OWL_DOCMANAG_H)
  13. # include <owl/docmanag.h>
  14. #endif
  15. #if !defined(OWL_OLEMDIFR_H)
  16. # include <owl/olemdifr.h>
  17. #endif
  18. #if !defined(OCF_OCDOC_H)
  19. # include <ocf/ocdoc.h>
  20. #endif
  21. #if !defined(OCF_OCAPP_H)
  22. # include <ocf/ocapp.h>
  23. #endif
  24. #if !defined(OWL_OLEDOC_H)
  25. # include <owl/oledoc.h>
  26. #endif
  27. #if !defined(OWL_OLEFRAME_H)
  28. # include <owl/oleframe.h>
  29. #endif
  30. #if !defined(OWL_OLEVIEW_H)
  31. # include <owl/oleview.h>
  32. #endif
  33.  
  34. OWL_DIAGINFO;
  35.  
  36. //
  37. //
  38. //
  39. TOleDocument::TOleDocument(TDocument* parent)
  40. :
  41.   TStorageDocument(parent),
  42.   OcDoc(0),
  43.   Closing(false)
  44. {
  45. }
  46.  
  47. //
  48. // For an OLE container the compound file remains open
  49. // until the application shuts down
  50. //
  51. TOleDocument::~TOleDocument()
  52. {
  53.   delete OcDoc;
  54. }
  55.  
  56. //
  57. // Prepare document shutdown
  58. //
  59. bool
  60. TOleDocument::CanClose()
  61. {
  62.     //
  63.     // if it's an open edit dll stop the closing process
  64.   TView* curView = GetViewList();
  65.   while (curView) {
  66.       // get the ole view
  67.     TOleView* oleView = TYPESAFE_DOWNCAST(curView, TOleView);
  68.     if (oleView && oleView->IsOpenEditing() && !GetOcApp()->IsOptionSet(amExeMode)) {
  69.         TOleFrame* olefr = TYPESAFE_DOWNCAST(oleView->GetApplication()->GetMainWindow(), TOleFrame);
  70.         CHECK(olefr);
  71.         olefr->ShowWindow(SW_HIDE);
  72.         oleView->OleShutDown();
  73.         return false; // don't close
  74.     }
  75.     curView = curView->GetNextView();
  76.   }
  77.  
  78.   // Just say yes if we are already in the closing process, or are embedded,
  79.   // or have multiple views open
  80.   //
  81.  
  82.   if (Closing || IsEmbedded())
  83.     return true;
  84.  
  85.   return TDocument::CanClose();
  86. }
  87.  
  88. //
  89. //  Shut down the TOleView's
  90. //
  91. void
  92. TOleDocument::OleViewClose()
  93. {
  94.   TView* curView = GetViewList();
  95.   while (curView) {
  96.     TOleView* oleView = TYPESAFE_DOWNCAST(curView, TOleView);
  97.     if (oleView)
  98.       oleView->OleShutDown();
  99.  
  100.     curView = curView->GetNextView();
  101.   }
  102. }
  103.  
  104. //
  105. // Close the compound file
  106. //
  107. bool
  108. TOleDocument::Close()
  109. {
  110.   // Make sure that TOleView's are closed first
  111.   //
  112.   OleViewClose();
  113.   OcDoc->Close();
  114.   return TStorageDocument::Close();
  115. }
  116.  
  117. //
  118. // Close the OLE document when the server is done with the
  119. // given IStorage from its container
  120. //
  121. bool
  122. TOleDocument::ReleaseDoc()
  123. {
  124.   PRECONDITION(OcDoc);
  125.  
  126.   TStorageDocument::ReleaseDoc();
  127.   OcDoc->SetStorage((IStorage*)0);
  128.  
  129.   return true;
  130. }
  131.  
  132. //
  133. // Open the OLE document when the server is provided with an
  134. // IStorage from its container
  135. //
  136. bool
  137. TOleDocument::SetStorage(IStorage* stg, bool remember)
  138. {
  139.   PRECONDITION(OcDoc);
  140.  
  141.   // If a storage is provided, then we are now using container's IStorage
  142.   //
  143.   if (stg)
  144.     Embedded = true;
  145.  
  146.   OcDoc->SetStorage(stg, remember);
  147.   TStorageDocument::SetStorage(stg, remember);
  148.  
  149.   return true;
  150. }
  151.  
  152. //
  153. // Restores the original root IStorage before the save operation
  154. //
  155. bool
  156. TOleDocument::RestoreStorage()
  157. {
  158.   PRECONDITION(OcDoc);
  159.  
  160.   OcDoc->RestoreStorage();
  161.   TStorageDocument::RestoreStorage();
  162.  
  163.   return true;
  164. }
  165.  
  166. //
  167. // Set the initial open mode
  168. //
  169. void
  170. TOleDocument::PreOpen()
  171. {
  172.   SetOpenMode(ofReadWrite | ofTransacted);
  173. }
  174.  
  175. //
  176. // Open the compound file so that we have an IStorage for use
  177. // with embedded objects. A document partner is created
  178. // to handle OLE related stuff.
  179. //
  180. bool
  181. TOleDocument::InitDoc()
  182. {
  183.   if (IsOpen())
  184.     return true; // compound file already open
  185.  
  186.   // Give user a chance to set a different open mode
  187.   //
  188.   PreOpen();
  189.  
  190.   if (GetDocPath())
  191.     SetOpenMode(GetOpenMode() | ofNoCreate);
  192.   else
  193.     SetOpenMode(GetOpenMode() | ofTemporary);
  194.  
  195.   if (TStorageDocument::Open(GetOpenMode(), GetDocPath())) {
  196.     if (OcDoc) { // use the existing ocdoc
  197.       OcDoc->SetStorage(StorageI);
  198.     }
  199.     else if (GetOcApp()) {
  200.       OcDoc = new TOcDocument(*GetOcApp(), GetDocPath(), StorageI);
  201.     }
  202.  
  203.     return true;
  204.   }
  205.  
  206.   return false;
  207. }
  208.  
  209. //
  210. // Save the embedded objects, if any
  211. //
  212. bool
  213. TOleDocument::Commit(bool force)
  214. {
  215.   if (Write())
  216.     return TStorageDocument::Commit(force);
  217.   else
  218.     return false;
  219. }
  220.  
  221. //
  222. // Load the embedded objects, if any
  223. //
  224. bool
  225. TOleDocument::Open(int, const char far* path)
  226. {
  227.   if (path)
  228.     SetDocPath(path);
  229.  
  230.   return Read();
  231. }
  232.  
  233. //
  234. // Check if current document path is the same as the
  235. // OcDoc's.
  236. //
  237. bool TOleDocument::PathChanged()
  238. {
  239.   string::set_case_sensitive(false);
  240.   return OcDoc->GetName() != string(GetDocPath());
  241. }
  242.  
  243. //
  244. // Save embed objects to the compound file
  245. //
  246. bool
  247. TOleDocument::Write()
  248. {
  249.   // Switch to new storage if path has changed & it is permanent ("SaveAs")
  250.   //
  251.   IStorage* newStorageI;
  252.   bool saveAs = PathChanged() && !OrgStorageI;      // also is 'remember'
  253.   bool sameAsLoad = !PathChanged() && !OrgStorageI; // use current storage
  254.   if (saveAs) {
  255.     // Update link monikers
  256.     //
  257.     string newName(GetDocPath());
  258.     OcDoc->SetName(newName);
  259.  
  260.     if (IsEmbedded())
  261.       newStorageI = StorageI; // Use the one assigned by container
  262.     else
  263.       newStorageI = GetNewStorage();
  264.   }
  265.   else
  266.     newStorageI = StorageI;
  267.  
  268.   return newStorageI ?
  269.     OcDoc->SaveParts(newStorageI, sameAsLoad, saveAs) :
  270.     false;
  271. }
  272.  
  273. //
  274. // Load embed objects from the compound file
  275. //
  276. bool
  277. TOleDocument::Read()
  278. {
  279.   // Load the embedded objects, if any
  280.   //
  281.   return OcDoc->LoadParts();
  282. }
  283.  
  284. //
  285. // Revert to last saved compound file
  286. //
  287. bool
  288. TOleDocument::Revert(bool clear)
  289. {
  290.   if (!StorageI)
  291.     return true;                    // return OK if storage already released
  292.  
  293.   if (!TDocument::Revert(clear) || !ReleaseDoc())
  294.     return false;
  295.  
  296.   if (!clear) {
  297.     InitDoc();
  298.     Open(0);
  299.   }
  300.  
  301.   SetDirty(false);
  302.   return true;
  303. }
  304.  
  305. //
  306. // Get OCF application partner
  307. //
  308. TOcApp*
  309. TOleDocument::GetOcApp()
  310. {
  311.   TOleFrame* olefr = TYPESAFE_DOWNCAST(GetDocManager().GetApplication()->GetMainWindow(), TOleFrame);
  312.  
  313.   return olefr->GetOcApp();
  314. }
  315.